class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int left = 0, right = nums.size() - 1, end = left + 1;
        vector<vector<int>> v;
        while (left < right - 1)
        {
            if (left > 0 && nums[left] == nums[left - 1])
            {
                left++;
                continue;
            }
            end = left + 1;
            right = nums.size() - 1;
            while (end < right)
            {
                int sum = nums[right];
                if (nums[left] + nums[end] + sum == 0)
                {
                    v.push_back({ nums[left],nums[end],nums[right] });
                    end++;
                    right--;
                    while (end < right && nums[end] == nums[end - 1]) end++;
                    while (end < right && nums[right] == nums[right + 1]) right--;
                }
                else if (nums[left] + nums[end] + sum > 0)
                {
                    right--;
                }
                else
                {
                    end++;
                }
            }
            left++;
        }
        return v;
    }
};